package com.example.iread.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.iread.R;
import com.example.iread.http.UserApi;
import com.example.iread.util.ToastUtil;
import com.example.iread.util.WindowUtil;

import java.util.HashMap;
import java.util.Map;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    EditText et_register_username, et_register_password;
    private TextView tv_register_brith;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        WindowUtil.setTransparentStatusBar(getWindow());

        et_register_username = findViewById(R.id.et_register_username);
        et_register_password = findViewById(R.id.et_register_password);
        tv_register_brith = findViewById(R.id.tv_register_brith);
        tv_register_brith.setOnClickListener(v -> {
            DatePickerDialog datePickerDialog = new DatePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT);
            datePickerDialog.setOnDateSetListener(((view, year, month, day) -> {
                String birth = String.format("%s-%s-%s",
                        year,
                        month + 1 < 10 ? "0" + (month + 1) : month + 1,
                        day < 10 ? "0" + day : day);
                tv_register_brith.setText(birth);
            }));
            datePickerDialog.show();
        });

        Button btn_register = findViewById(R.id.btn_register);
        btn_register.setOnClickListener(this);

        TextView tv_sign_in = findViewById(R.id.tv_sign_in);
        tv_sign_in.setOnClickListener(v -> {
            Intent intent = new Intent(this, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        });
    }

    @Override
    public void onClick(View v) {
        String username = et_register_username.getText().toString();
        String password = et_register_password.getText().toString();
        String birth = tv_register_brith.getText().toString();

        if (isEmpty(username) || isEmpty(password) || isEmpty(birth)) {
            ToastUtil.show(this, "Input error, please check");
            return;
        }

        Map<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        params.put("birthday", birth);

        UserApi.register(params, res -> {
            Intent intent = new Intent(this, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            ToastUtil.show(this, "Registration successful, please log in");
        });

        
    }

    private boolean isEmpty(String s) {
        return s == null || "".equals(s);
    }
}